package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author hu
 * @date 2025/5/21
 */
@RestController
@Slf4j
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {

    @Resource
    private DishService dishService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     */
    @ApiOperation("新增菜品")
    @PostMapping("/dish")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);
        String key = "dish:category:" + dishDTO.getCategoryId();
        clearCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO queryDTO) {
        log.info("菜品分页查询：{}", queryDTO);
        PageResult pageResult = dishService.pageQuery(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品批量删除
     */
    @ApiOperation("菜品批量删除")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteBatch(ids);
        String key = "dish:category:*";
        clearCache(key);
        return Result.success();
    }

    /**
     * 根据ID查询菜品
     */
    @ApiOperation("根据ID查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        log.info("根据ID查询菜品：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 根据ID修改菜品与口味
     */
    @ApiOperation("根据ID修改菜品与口味")
    @PutMapping
    public Result updateById(@RequestBody DishDTO dishDTO) {
        log.info("根据ID修改菜品与口味：{}", dishDTO);
        dishService.updateByIdWithFlavor(dishDTO);
        String key = "dish:category:*";
        clearCache(key);
        return Result.success(dishDTO);
    }

    /**
     * 修改菜品售卖状态-起售，停售
     */
    @ApiOperation("修改菜品售卖状态")
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("修改菜品售卖状态：{},{}", status, id);
        dishService.updateStatus(status, id);
        String key = "dish:category:*";
        clearCache(key);
        return Result.success();
    }

    /**
     * 根据分类ID查询菜品
     */
    @ApiOperation("根据分类ID查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getByCategoryId(Long categoryId,String name) {
        log.info("根据套餐ID查询菜品：{},{}", categoryId,name);
        List<Dish> dishList = dishService.getByCategoryId(categoryId,name);
        return Result.success(dishList);
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
